package com.cheng.reggie.service.impl;

import com.cheng.reggie.dto.SetmealDto;
import com.cheng.reggie.entity.*;
import com.cheng.reggie.exception.BusinessException;
import com.cheng.reggie.mapper.CategoryMapper;
import com.cheng.reggie.mapper.SetmealDishMapper;
import com.cheng.reggie.mapper.SetmealMapper;
import com.cheng.reggie.service.SetmealService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {


    /**
     * 根据套餐id修改套餐数据
     *
     * @param setmealDto
     * @return boolean
     */
    @Transactional
    @CacheEvict(value = "setmealCache",allEntries = true )
    @Override
    public boolean updateById(SetmealDto setmealDto) {
        //1.完善数据
        setmealDto.setUpdateTime(LocalDateTime.now());
        //2.修改菜品
        setmealMapper.updateById(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> setmealDishList = setmealDishes.stream().map((setmealDish -> {
            // 获取套餐主键,设置修改人,创建人,修改时间,创建时间,
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setSort(0);
            //批量保存
            return  setmealDish;

        })).collect(Collectors.toList());
        setmealDishMapper.save(setmealDishList);
        return true;
    }

    /**
     * //根据套餐id查询套餐数据
     *
     * @param id
     * @return Setmeal
     */
    @Override
    public Setmeal findById(Long id) {
        return   setmealMapper.findById(id);
    }

    /**
     * 根据类别id查询套餐列表数据
     *
     * @param categoryId
     * @param status
     * @return List
     */
    @Override
    //查询写入缓存
    //@Cacheable(value = "setmealcache",key = "#categoryId+'_'+#status",unless = "#result==null")
    public List<Setmeal> findBySetmealId(Long categoryId, Integer status) {
        return setmealMapper.findBySetmealId(categoryId, status);

    }

    /**
     * 删除套餐业务方法
     *
     * @param ids     要删除的套餐id列表数据
     * @param updateUser 更新人
     * @return boolean
     */
    @Transactional
    @CacheEvict(value = "setmealCache" )
    @Override
    public boolean deleteByIds(List<Long> ids, Long updateUser) {
        //1.判断传递过来的套餐id列表是否存在启用的套餐
        //查询启动的个数,个数大于0代表存在,否则不存在
        Integer count = setmealMapper.countByIds(ids);
        //存在,则抛异常,提示用户:"无法删除起售的套餐"
        if (count >0) {
            throw  new BusinessException("无法删除起售的套餐");
        }
        //批量删除套餐菜品相关数据
        setmealDishMapper.deleteBySetmealIds(ids,updateUser,LocalDateTime.now());
        //批量删除套餐
        setmealMapper.deleteByIds(ids,updateUser,LocalDateTime.now());
        return true;
    }

    /**
     * 套餐分页查询数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findPage(Integer page, Integer pageSize, String name) {
        //1.PageHelper设置查询第几页和每页大小
        PageHelper.startPage(page,pageSize);

        //2.执行查询所有的套餐数据
        List<Setmeal> setmealList = setmealMapper.findAll(name);
        //3封装pageInfo对象
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        //4.使用当前数据列表中每个套餐封装给setmeal对象
        List<SetmealDto> setmealDtoList = setmealList.stream().map((setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            //将setmeal套餐对象数据赋值给SetmealDto里面属性数据
            BeanUtils.copyProperties(setmeal,setmealDto);
            //获取类别别名名称
            Category category = categoryMapper.findById(setmealDto.getCategoryId());
            //获取类别别名名称赋值给setmealDto
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        })).collect(Collectors.toList());
        //封装层前端需要Page
        Page<SetmealDto> pageObj = new Page<>();
        pageObj.setRecords(setmealDtoList);
        pageObj.setTotal(pageInfo.getTotal());
        //返回
        return pageObj;
    }

    @Autowired(required = false)
    private SetmealMapper setmealMapper;


    @Autowired(required = false)
    private  SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    /**
     * 添加套餐业务方法
     *
     * @param setmealDto
     * @return boolean
     */
    @Transactional //开启事务
    @CacheEvict(value = "setmealCache",allEntries = true )
    @Override
    public boolean save(SetmealDto setmealDto) {
        //1.完善套餐数据(状态,创建时间,更新时间)
        setmealDto.setStatus(1);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        //2.调用数据访问层新增套餐
        setmealMapper.save(setmealDto);

        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes().stream().map((setmealDish -> {
            //(设置套餐主键,创建时间,创建人,更新人,更新顺序,排序)
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            return setmealDish;
        })).collect(Collectors.toList());
        //4.调用数据库访问层批量插入套餐菜品列表数据
        setmealDishMapper.save(setmealDishList);

        return true;
    }
}
